package com.kaesun.ufit.weight;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.kaesun.ufit.hardware.Buzzer;
import com.kaesun.ufit.tools.ConfigData;
import com.kaesun.ufit.tools.SummaryView;
import com.kaesun.ufit.tools.ToolbarView;

public class SummaryActivity extends Activity implements View.OnClickListener, IOnHandlerMessage {

	private static final int BUTTON_IDS[] = {
		R.id.button_back, R.id.button_warning, R.id.button_guide,
	};

	private static final int REQUEST_WARNING = 1;
	private static final int REQUEST_GUIDE = 2;

	private MainApplication mApp = null;
	private WeakRefHandler mHandler = null;
	private ToolbarView mToolbarView = null;

	private SummaryView mSummaryView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_summary);

		for (int i=0; i<BUTTON_IDS.length; i++) {
			findViewById(BUTTON_IDS[i]).setOnClickListener(this);
		}

		mApp = (MainApplication)getApplication();
		mHandler = new WeakRefHandler((IOnHandlerMessage)this);
		mToolbarView = (ToolbarView)findViewById(R.id.view_toolbar);

		mSummaryView = (SummaryView)findViewById(R.id.view_summary);

		mToolbarView.setTitle("Summary");

		int mode = getIntent().getIntExtra(ExcerciseActivity.EXTRA_EXCERCISE_MODE, ExcerciseActivity.MODE_FREE);
		Log.e("aaa", "mode : " + mode);
		switch (mode) {
		case ExcerciseActivity.MODE_FREE:
			if (false == mApp.wiFiSendFreeExcercise()) {
			}
			break;
		case ExcerciseActivity.MODE_MEMBER:
			if (false == mApp.wiFiSendExcercise()) {
			}
			break;
		case ExcerciseActivity.MODE_ONERM:
			if (false == mApp.wiFiSendMeasure()) {
			}
			break;
		}

		if (ExcerciseActivity.MODE_FREE == mode) {
			float weight[] = {0, 0, 0};
			float calorie[] = {0, 0, 0};
			switch (mApp.mExcerciseData.mMeasureSet) {
			case 1:
				weight[0] = mApp.mExcerciseData.mMeasureWeight;
				calorie[0] = mApp.mExcerciseData.mMeasureCalories;
				break;
			case 2:
				weight[0] = mApp.mExcerciseData1.mMeasureWeight;
				calorie[0] = mApp.mExcerciseData1.mMeasureCalories;
				weight[1] = mApp.mExcerciseData.mMeasureWeight;
				calorie[1] = mApp.mExcerciseData.mMeasureCalories;
				break;
			case 3:
				weight[0] = mApp.mExcerciseData1.mMeasureWeight;
				calorie[0] = mApp.mExcerciseData1.mMeasureCalories;
				weight[1] = mApp.mExcerciseData2.mMeasureWeight;
				calorie[1] = mApp.mExcerciseData2.mMeasureCalories;
				weight[2] = mApp.mExcerciseData.mMeasureWeight;
				calorie[2] = mApp.mExcerciseData.mMeasureCalories;
				break;
			}
			mSummaryView.setValue(mApp.mExcerciseData.mMeasureRate, weight, calorie,
					mApp.mExcerciseData.mMeasureCount, mApp.mExcerciseData.mMeasureSet);
		} else {
			mSummaryView.setValue(mApp.mExcerciseData.mMeasureRate, mApp.mExcerciseData.mMeasureWeight,
					mApp.mExcerciseData.mMeasureCalories,mApp.mExcerciseData.mMeasureCount, mApp.mExcerciseData.mMeasureSet);
		}
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	protected void onPause() {

		super.onPause();
	} 

	@Override
	protected void onResume() {
		mApp.setHandler(mHandler, true);

		checkLanguage();

		super.onResume();
	}

	public void onClick(View arg0) {
		mApp.timeoutReset();

		Buzzer.delay(50);
		switch (arg0.getId()) {
		case R.id.button_back:
			setResult(RESULT_OK);
			finish();
			break;
		case R.id.button_warning:
			showActivity(REQUEST_WARNING);
			break;
		case R.id.button_guide:
			showActivity(REQUEST_GUIDE);
			break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mApp.timeoutReset();

		return super.onTouchEvent(event);
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case MainApplication.HANDLER_TIMEOUT_SECOND:
			mToolbarView.setTime(msg.arg1, msg.arg2);
			break;
		case MainApplication.HANDLER_TIMEOUT_ACTIVITY:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REQUEST_WARNING:
			if (RESULT_OK != resultCode) {
				showActivity(REQUEST_GUIDE);
			}
			break;
		case REQUEST_GUIDE:
			if (RESULT_OK != resultCode) {
				showActivity(REQUEST_WARNING);
			}
			break;
		}
	}


	private void showActivity(int req) {
		Intent intent = null;

		switch (req) {
		case REQUEST_WARNING:
			intent = new Intent(SummaryActivity.this, WarningActivity.class);
			break;
		case REQUEST_GUIDE:
			intent = new Intent(SummaryActivity.this, GuideActivity.class);
			break;
		}

		if (null != intent) {
			startActivityForResult(intent, req);
		}
	}

	private int _serviceLanguage = ConfigData.DEF_SERVICE_LANGUAGE;
	private void checkLanguage() {
		int language = ConfigData.getPrefInteger(this, ConfigData.PREF_SERVICE_LANGUAGE, ConfigData.DEF_SERVICE_LANGUAGE);
		if (_serviceLanguage == language) {
			return;
		}
		_serviceLanguage = language;

		int imgId = 0;
		switch (_serviceLanguage) {
		case ConfigData.SERVICE_LANGUAGE_ENG:
			imgId = R.drawable.img_summary_eng;
			break;
		case ConfigData.SERVICE_LANGUAGE_KOR:
		default:
			imgId = R.drawable.img_summary;
			break;
		}
		if (0 < imgId) {
			((ImageView)findViewById(R.id.image_summary)).setImageResource(imgId);
		}
	}
}
